package com.dingpeikeji.quickdeer.modules.rentproduct.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.DictUtils;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.DemandsPriceDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TDemandsDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TRentproductDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.DemandsReact;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TDemands;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;

/**
 * 用户需求Service
 */
@Service
@Transactional(readOnly = true)
public class TDemandsService extends CrudService<TDemandsDao, TDemands> {
	
	@Autowired
	private DemandsPriceDao demandsPriceDao;
	@Autowired
	private TRentproductDao rentproductDao;

	public TDemands get(String demandid) {
		return super.get(demandid);
	}
	
	public List<TDemands> findList(TDemands tDemands) {
		return super.findList(tDemands);
	}
	
	public Page<TDemands> findPage(Page<TDemands> page, TDemands tDemands) {
		// 查找用户创建的需求
		tDemands.setCreateBy(UserUtils.getUser());
		return super.findPage(page, tDemands);
	}
	
	@Transactional(readOnly = false)
	public void save(TDemands tDemands) {
		if(tDemands.getDemandid() == null) {
			tDemands.setCreateBy(UserUtils.getUser());
			tDemands.setCreateDate(new Date());
			dao.insert(tDemands);
		} else {
			dao.update(tDemands);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(TDemands tDemands) {
		super.delete(tDemands);
	}
	
	/**
	 * 需求确定
	 */
	@Transactional(readOnly = false)
	public boolean sure(TDemands tDemands) throws Exception{
		
		// 更改需求状态
		tDemands.setReactstatus(TDemands.REACTSTATUS_SURE);
		if(dao.update(tDemands)==0){
			throw new RuntimeException();
		}
		DemandsReact demandsReact = new DemandsReact();
		demandsReact.setDemandid(tDemands.getDemandid());
		List<DemandsReact> demandsReactList = demandsPriceDao.findDemandsReactList(demandsReact);
		if(demandsReactList==null||demandsReactList.isEmpty()){
			throw new RuntimeException();
		}
		demandsReact = demandsReactList.get(0);
		// 生成整车发布信息（定向发布）
		// yes_no数据字典的取值
//		String yes = DictUtils.getDictValue("是","yes_no","");
		String no = DictUtils.getDictValue("否","yes_no","");
		String onlinePay = DictUtils.getDictValue("线上结算","T_PAY","");
		TRentproduct rentproduct = new TRentproduct();
		rentproduct.setOffice(new Office(demandsReact.getReactofficeid()));
		rentproduct.setProductname("用户需求"); // 产品名称字段无来源
		TCargotype cargotype = new TCargotype();
		cargotype.setCargotypeid(demandsReact.getCartype());
		rentproduct.setCargotype(cargotype);
		rentproduct.setDepartcity(tDemands.getDepartcity());
		rentproduct.setDestcity(tDemands.getDestcity());
		rentproduct.setFromdate(tDemands.getFromdate());
		// 终止日期根据开始日期+出租天数
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(tDemands.getFromdate());
		calendar.add(Calendar.DAY_OF_MONTH, tDemands.getRentdays());
		rentproduct.setStopdate(calendar.getTime());
		rentproduct.setPayway(onlinePay);
		rentproduct.setSuitrtnprice(tDemands.getHopeprice());
		rentproduct.setSuitonewayprice(null);
		rentproduct.setSuitfreeprice(null);
		rentproduct.setKms(100D); // 公里数无来源
		rentproduct.setLasthour(2D); // 时长无来源
		rentproduct.setIsroadtoll(no);
		rentproduct.setIsparkfee(no);
		rentproduct.setOuttimefee(null);
		rentproduct.setSuitdesc(null);
		rentproduct.setVehiclenum(tDemands.getVehiclenum());
		rentproduct.setReleasegroup(null);
		rentproduct.setReleaseobject(tDemands.getReleaseobject());
		rentproduct.setNightquota(null);
		rentproduct.setIsall(no);
		rentproduct.setIspublic(tDemands.getIspublic());
		rentproduct.setDelFlag(TRentproduct.DEL_FLAG_NORMAL);
		rentproduct.setCreateBy(UserUtils.getUser());
		rentproduct.setCreateDate(new Date());
		if(rentproductDao.insert(rentproduct)==0){
			throw new RuntimeException();
		}
		return true;
	}
	
}